package com.leyou.cart.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.auth.pojo.UserInfo;
import com.leyou.cart.inteceptors.LoginInterceptor;
import com.leyou.cart.pojo.Cart;
import com.leyou.common.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartService {


    @Autowired
    private StringRedisTemplate redisTemplate;


    static final String KEY_PREFIX = "ly:cart:uid:";

    /**
     *
     * 判断是否已经加入过，如果已经加入过，则直接改值，如果没有，直接添加
     *
     * 考虑数据结构
     *
     * Map<userId,Map<skuId,sku>>
     * @param cart
     */
    public void addCart(Cart cart) {

        //从拦截器中获取用户的信息
        UserInfo userInfo = LoginInterceptor.getUserInfo();

        //要根据当前用户的id获取用户的购物车信息,ops是当前用户的购物车信息
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + userInfo.getId());


        String storeCart = (String) ops.get(cart.getSkuId()+"");

        if (!StringUtils.isNotBlank(storeCart)){//没加过,此时加入时一定要把cart对象转为String
            ops.put(cart.getSkuId()+"", JsonUtils.serialize(cart));

        }else {
            Cart storeCartObj = JsonUtils.nativeRead(storeCart, new TypeReference<Cart>() {
            });

            //原来的数量加上现在数量
            storeCartObj.setNum(storeCartObj.getNum()+cart.getNum());

            ops.put(storeCartObj.getSkuId()+"", JsonUtils.serialize(storeCartObj));
        }

        System.out.println("storeCart = " + storeCart);

    }


    public List<Cart> queryCarts() {
        //从拦截器中获取用户的信息
        UserInfo userInfo = LoginInterceptor.getUserInfo();

        //要根据当前用户的id获取用户的购物车信息,ops是当前用户的购物车信息
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + userInfo.getId());


        //获取当前用户的所有的sku的string信息
        List<Object> strValues = ops.values();

        List<Cart> carts = new ArrayList<>();

        //遍历String数组，
        if (null!=strValues){
            strValues.forEach(strValue->{
                //把每个string的值转换为java对象
                Cart cart = JsonUtils.parse(strValue.toString(), Cart.class);

                carts.add(cart);
            });

            return carts;
        }

        return null;
    }

    public void deleteCartById(Long id) {
        //从拦截器中获取用户的信息
        UserInfo userInfo = LoginInterceptor.getUserInfo();

        //要根据当前用户的id获取用户的购物车信息,ops是当前用户的购物车信息
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + userInfo.getId());

        ops.delete(id.toString());
    }

    public void increment(Long id) {
        //从拦截器中获取用户的信息
        UserInfo userInfo = LoginInterceptor.getUserInfo();

        //要根据当前用户的id获取用户的购物车信息,ops是当前用户的购物车信息
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + userInfo.getId());

        //根据用户和skuId获取具体的cart对象的string值
        Object o = ops.get(id.toString());

        //把得到的string值转换为Cart对象
        Cart cart = JsonUtils.parse(o.toString(), Cart.class);

        //改变cart对象的数值
        cart.setNum(cart.getNum()+1);

        //重新存储cart到redis
        ops.put(cart.getSkuId().toString(),JsonUtils.serialize(cart));

    }
}
